package com.winner.data.util;

import java.io.File;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import cn.hutool.core.io.FileUtil;

/**
 * mybatis-plus代码生成器, 支持mysql和达梦等
 *
 * @author wangxinle5
 * @since 2022/04/25
 */
public class MyBatisPlusCodeFileGenerate {

    /** 一次生成多个表, 请用逗号隔开即可 */
    private static final String TABLES = "user";

    /** 会忽略 tbl_user 的前缀tbl_, 用剩下的user生成文件名是UserController */
    private static final String TABLE_PREFIX = "tbl_";

    private static final String BASE_PACKAGE = "com.winner.mybatisplus";

    /** 就是在basePackage目录下再加一个MODULE_NAME的目录, 一般不需要 */
    public static final String MODULE_NAME = "";
    private static final String URL = "jdbc:mysql://localhost:3306/test";
    private static final String USER_NAME = "root";
    private static final String PASSWORD = "123456";
    private static final String BASE_PROJECT_PATH = "D:\\MP";
    private static final String AUTHOR_NAME = "wangxinle5";

    public static void main(String[] args) {
        // 每次执行前先删除之前的目录文件
        FileUtil.del(new File(BASE_PROJECT_PATH));

        FastAutoGenerator.create(URL, USER_NAME, PASSWORD).globalConfig(builder -> {
            builder.author(AUTHOR_NAME).outputDir(BASE_PROJECT_PATH) // 实体属性 Swagger2 注解 .enableSwagger()
                .commentDate("yyyy-MM-dd").dateType(DateType.ONLY_DATE);
        }).packageConfig(builder -> {
            builder.parent(BASE_PACKAGE).moduleName(MODULE_NAME).controller("controller").entity("entity")
                .mapper("mapper").xml("mapper.xml").service("service").serviceImpl("service.impl").pathInfo(null);
        }).strategyConfig(builder -> {
            builder.addInclude(TABLES) // 需要生成代码的表名
                .addTablePrefix(TABLE_PREFIX) // 表名前缀
                .entityBuilder().enableLombok().controllerBuilder().enableRestStyle()
                // .enableTableFieldAnnotation()
                .enableHyphenStyle().controllerBuilder().enableRestStyle().enableHyphenStyle().mapperBuilder()
                .enableBaseResultMap().enableBaseColumnList().enableMapperAnnotation().serviceBuilder()
                .formatServiceFileName("%sService").formatServiceImplFileName("%sServiceImpl");
        }).templateEngine(new FreemarkerTemplateEngine())
            // 不写此行, 默认是Velocity引擎模板 new VelocityTemplateEngine()
            .execute();

    }
}
